package com.ruoyi.business.service.impl;

import com.ruoyi.business.domain.NoticeInfo;
import com.ruoyi.business.mapper.NoticeInfoMapper;
import com.ruoyi.business.service.INoticeInfoService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.vo.SystemItemVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class NoticeInfoServiceImpl implements INoticeInfoService {
    @Autowired
    private NoticeInfoMapper noticeInfoMapper;

    /**
     * 查询通知公告
     *
     * @param noticeId 通知公告主键
     * @return 通知公告
     */
    @Override
    public NoticeInfo selectNoticeInfoByNoticeId(Integer noticeId) {
        return noticeInfoMapper.selectNoticeInfoByNoticeId(noticeId);
    }

    /**
     * 查询通知公告列表
     *
     * @param noticeInfo 通知公告
     * @return 通知公告
     */
    @Override
    public List<NoticeInfo> selectNoticeInfoList(NoticeInfo noticeInfo) {
        return noticeInfoMapper.selectNoticeInfoList(noticeInfo);
    }

    /**
     * 新增通知公告
     *
     * @param noticeInfo 通知公告
     * @return 结果
     */
    @Override
    public int insertNoticeInfo(NoticeInfo noticeInfo) {
        noticeInfo.setCreateTime(DateUtils.getNowDate());
        return noticeInfoMapper.insertNoticeInfo(noticeInfo);
    }

    /**
     * 修改通知公告
     *
     * @param noticeInfo 通知公告
     * @return 结果
     */
    @Override
    public int updateNoticeInfo(NoticeInfo noticeInfo) {
        noticeInfo.setUpdateTime(DateUtils.getNowDate());
        return noticeInfoMapper.updateNoticeInfo(noticeInfo);
    }

    /**
     * 批量删除通知公告
     *
     * @param noticeIds 需要删除的通知公告主键
     * @return 结果
     */
    @Override
    public int deleteNoticeInfoByNoticeIds(Integer[] noticeIds) {
        return noticeInfoMapper.deleteNoticeInfoByNoticeIds(noticeIds);
    }

    /**
     * 删除通知公告信息
     *
     * @param noticeId 通知公告主键
     * @return 结果
     */
    @Override
    public int deleteNoticeInfoByNoticeId(Integer noticeId) {
        return noticeInfoMapper.deleteNoticeInfoByNoticeId(noticeId);
    }

    /**
     * 修改阅读状态
     *
     * @param noticeId 数据主键
     * @return 结果
     */
    public int updateNoticeMessageReadStatus(Long noticeId) {
        return noticeInfoMapper.updateNoticeMessageReadStatus(noticeId);
    }

    //推送系统数据
    private static final ArrayList<SystemItemVo> itemVos = new ArrayList<>();

    static {
        itemVos.add(new SystemItemVo("Mon", 10));
        itemVos.add(new SystemItemVo("Tue", 1));
        itemVos.add(new SystemItemVo("Web", 12));
        itemVos.add(new SystemItemVo("Thu", 124));
        itemVos.add(new SystemItemVo("Fri", 122));
        itemVos.add(new SystemItemVo("Sat", 43));
        itemVos.add(new SystemItemVo("Sun", 13));
    }

    @Override
    public Map<String, Object> sendSystemData() {
        Random rand = new Random();
        List<Integer> nums = itemVos.stream()
                .map(itemVo -> {
                    int change = rand.nextInt(41) - 20; // ±20的随机变化
                    return Math.max(0, itemVo.getNum() + change); // 最小值保持0
                })
                .collect(Collectors.toList());
        // 提取所有age到新列表
       /* List<Integer> nums = itemVos.stream()
                .map(SystemItemVo::getNum)
                .collect(Collectors.toList());*/
        HashMap<String, Object> map = new HashMap<>();
        map.put("yData", nums);
        return map;


    }

}
